Skip to content

fix(many): fix "not a valid selector" exception when an option ID contains quotes#2000

Merged
matyasf merged 1 commit into
masterfrom
fix_id_crash
Jun 3, 2025
Merged

fix(many): fix "not a valid selector" exception when an option ID contains quotes#2000
matyasf merged 1 commit into
masterfrom
fix_id_crash

Conversation

@matyasf

@matyasf matyasf commented Jun 2, 2025

Copy link
Copy Markdown
Collaborator

The issue is that we are letting users choose any string as an ID, and the querySelector method only accepts CSS safe IDs. Luckily there is a built in method to escape stuff that it does not like

Fixes INSTUI-4570

To test:

  • This code should not throw errors:
const options = ["Option 1", 'Option 2 with " inside']

render(
  <SimpleSelect renderLabel="Uncontrolled Select">
    {options.map((option) => (
      <SimpleSelect.Option id={option} value={option}>
        {option}
      </SimpleSelect.Option>
    ))}
  </SimpleSelect>
)
  • In the 'Video Settings' Drilldown example make an option's ID contain a double quote ("). when hovering over it the example should not crash

@matyasf matyasf requested a review from Copilot June 2, 2025 14:48
@matyasf matyasf self-assigned this Jun 2, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR applies CSS.escape to dynamic IDs used in querySelector calls to prevent “not a valid selector” exceptions when IDs contain special characters.

  • Escape IDs passed into querySelector across multiple components.
  • Add early-return guard for missing id or container in scrollToOption.
  • Restructure overflow tab lookup to use escaped selectors.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
packages/ui-top-nav-bar/src/TopNavBar/TopNavBarLayout/SmallViewportLayout/index.tsx Use CSS.escape on targetId in querySelector.
packages/ui-tabs/src/Tabs/index.tsx Guard tab lookup and escape id when querying overflowed tabs.
packages/ui-select/src/Select/index.tsx Add early return for missing values and escape id in scrollToOption.
packages/ui-drilldown/src/Drilldown/index.tsx Escape targetId in popover content lookup.
Comments suppressed due to low confidence (3)

packages/ui-top-nav-bar/src/TopNavBar/TopNavBarLayout/SmallViewportLayout/index.tsx:313

  • There are no tests verifying that IDs with quotes or other special characters are escaped correctly. Add unit or integration tests to cover these edge cases and ensure no selector errors occur.
[id="${CSS.escape(targetId)}"]

packages/ui-drilldown/src/Drilldown/index.tsx:1519

  • [nitpick] Ensure that CSS.escape is available in all target browsers or include a polyfill/fallback, as not all environments provide it natively.
querySelector(`#${CSS.escape(targetId)}`)

packages/ui-select/src/Select/index.tsx:295

  • The closing brace for scrollToOption was removed, leading to an unbalanced brace and syntax error. Add a matching } to properly close the method.
  }

@github-actions

github-actions Bot commented Jun 2, 2025

Copy link
Copy Markdown
PR Preview Action v1.6.1
Preview removed because the pull request was closed.
2025-06-03 11:31 UTC

@instructure instructure deleted a comment from Copilot AI Jun 2, 2025
@matyasf matyasf force-pushed the fix_id_crash branch 3 times, most recently from 0c6bf45 to c06b1c2 Compare June 2, 2025 19:37
@matyasf matyasf requested review from ToMESSKa and git-nandor June 2, 2025 19:46
Comment thread packages/ui-drilldown/src/Drilldown/__new-tests__/Drilldown.test.tsx Outdated
…tains quotes

The issue is that we are letting users choose any string as an ID, and the querySelector method only
accepts CSS safe IDs. Luckily there is a built in method to escape stuff that it does not like

Fixes INSTUI-4570
@matyasf matyasf requested a review from ToMESSKa June 3, 2025 09:05
@matyasf matyasf merged commit 78e0b96 into master Jun 3, 2025
12 checks passed
@matyasf matyasf deleted the fix_id_crash branch June 3, 2025 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants